- /* sxfcvbin.cpp by K.Tsuru */
- // function ID = 502 BRADIX
- /********************************************
- SDecimal class
- Provides radix conversion DRADIX --> BRADIX.
- ********************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
-
- SDecimal SDecimal::ConvToBin(const SDouble& m){
- SDecimal result; // result = 0;
- if(m.Type() == m.BIN_DEC){ //already converted
- result = m;
- return result;
- }
- if(m.Sign(502) == 0) return result;
- int e = m.NetRdxExp(); // m = 0.M*DRADIX^e
- // 1 1-e
- // m = 0.aaaa|bbbb|...*DRADIX^e = 0.0000|...|aaaa|bbbb|....
- // out of effective figures ?
- if( ( e < 0 ) && ( uint(1-e) > m.MaxSize() +1u) ) return result;
-
- //Check the integral part.
- double ipD;
- fType ip = 0;
- SDouble x(Dabs(m)); //It makes a copy.
- if(e >= 1){
- SDouble ipSD; //integral part
- x = Modf(x, ipSD); //It divides x into integer and decimal.
- ipD = doubleD(ipSD, 0); //convert into double
- if(ipD >= BRADIX) m.SetError(m.OVERFLOW_ERR,"SX ConvertToBin", 502);
- ip = (fType)ipD; // ip < BRADIX
- }
- //Here x has the decimal part only.
- if(x.Sign(502) == 0){ //integlal part only
- result.figure[0] = ip;
- result.SetSign((int)ip*m.Sign());
- return result;
- }
- //Conversion of decimal part
- int f = (int)x.First();
- const fType* xv = x.ReadFigures();
- fType* rv = result.figure.Elements();
- register int i = (int)x.Last();
- //value of exponent
- e = x.NetRdxExp(); // e is not positive.
- #ifndef NDEBUG
- assert(e <= 0);
- #endif
- result.SetSign(1);
- rv[0] = xv[i--];
-
- while(i >= f){
- //The function XsDiv() does not change the address "rv".
- XsDiv(result, DRADIX, result);
- if(xv[i]){
- result.aTail = 0;
- rv[0] += xv[i]; //rv[0] < BRADIX, xv[i]<DRADIX
- }
- i--;
- }
- /*
- The contribution from exponent.
- It divides by DRADIX (1-e) times for two reasons
- 1.converting finite decimal such as 2^(-10) exactly.
- 2.avoiding the multiplication by an infinite decimal 1/DRADIX in binary
- which costs time.
- */
- for(i = 0; i < 1-e; i++) XsDiv(result, DRADIX, result);
-
- int sgn=result.Sign(502); //maybe result == 0 by above division
- //It adds the integral part.
- if(ip){
- result.aTail = 0;
- result.figure[0] += ip;
- sgn=1;
- }
- if(sgn) result.SetSign( m.Sign() );
- return result;
- }
sxfcvbin.cpp : last modifiled at 2017/03/13 14:32:02(2,316 bytes)
created at 2015/12/22 16:09:56
The creation time of this html file is 2017/10/27 15:45:59 (Fri Oct 27 15:45:59 2017).